package gui.Teacher;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.*;
import javax.swing.border.TitledBorder;

import control.AddExamControl;

import BaseClasses.LogicalExam;
import BaseClasses.PracticalExam;
import BaseClasses.Question;
import BaseClasses.Subject;
import System.MainControl;
import System.SysMode;
import System.SystemObject;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Font;
import java.awt.SystemColor;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Date;
import java.util.Calendar;
import java.util.ListIterator;
import java.util.Map;

public class AddExamGui extends JPanel implements ActionListener{
	
	public JButton btnBack_1 ;
	public JButton btnAddExam;
	private JComboBox subcmbx;
	private JTextField textField_2;
	private AddExamControl control;
	private JTextField textField_3;
	private JTextField txtChooseQuestion;
	private PracticalExam exam;
	private JLabel lblEnterCourseName;
	private JSpinner spinner ;
	private JLabel lblEnterDuration;
	private JTextField textField;
	private JComboBox comboBox;
	private JButton btnAdd; 
	List<String> question;
	private JTextField textField_4;
	
	public AddExamGui() {
	    super();
	    setBackground(SystemColor.activeCaption);
	    control = new AddExamControl();
		initialize();
}


private void initialize() {
	
	this.setSize(500, 550);
	this.setLayout(null);
	setBorder(new TitledBorder(null, "Add Exam", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	JLabel lblChooseSubject = new JLabel("Choose Subject");
	lblChooseSubject.setHorizontalAlignment(SwingConstants.CENTER);
	lblChooseSubject.setFont(new Font("Tahoma", Font.BOLD, 14));
	lblChooseSubject.setBounds(10, 48, 132, 23);
	add(lblChooseSubject);
	
	subcmbx = new JComboBox();
	subcmbx.setBounds(205, 50, 86, 20);
	SystemObject sys = new SystemObject(MainControl.currentUser, SysMode.getSubject);
	control.sendToServer(sys);
	List<Subject> subjectList = null;
	SystemObject retObj = control.GetSysObj();
	if (retObj.getMode() == SysMode.OK)
		subjectList = (List<Subject>)retObj.getObj();
	
	if (subjectList != null)
	{
		for (int i=0;i<subjectList.size();i++)
		{
			subcmbx.addItem(subjectList.get(i).getName());
		}
	}
	add(subcmbx);
	
	
	JLabel lblSetExamId = new JLabel("Set Exam ID");
	lblSetExamId.setHorizontalAlignment(SwingConstants.CENTER);
	lblSetExamId.setFont(new Font("Tahoma", Font.BOLD, 14));
	lblSetExamId.setBounds(10, 96, 105, 14);
	add(lblSetExamId);
	
	textField_2 = new JTextField();
	textField_2.setBounds(205, 94, 86, 20);
	add(textField_2);
	textField_2.setColumns(10);
	btnAddExam = new JButton("Add Exam");
	btnAddExam.setEnabled(false);
	btnAddExam.addActionListener(this);
	btnAddExam.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
	btnAddExam.setBounds(52, 425, 130, 23);
	add(btnAddExam);
	
	btnBack_1 = new JButton("Back");
	btnBack_1.addActionListener(this);
	btnBack_1.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
	btnBack_1.setBounds(354, 425, 89, 23);
	add(btnBack_1);
	
	textField_3 = new JTextField();
	textField_3.setBounds(205, 139, 86, 20);
	add(textField_3);
	textField_3.setColumns(10);
	
	txtChooseQuestion = new JTextField();
	txtChooseQuestion.setEditable(false);
	txtChooseQuestion.setBackground(SystemColor.activeCaption);
	txtChooseQuestion.setFont(new Font("Tahoma", Font.BOLD, 14));
	txtChooseQuestion.setText("Choose Question and enter Number of Points:");
	txtChooseQuestion.setBounds(10, 262, 335, 20);
	add(txtChooseQuestion);
	txtChooseQuestion.setColumns(10);
	
	lblEnterCourseName = new JLabel("Enter Course Name");
	lblEnterCourseName.setFont(new Font("Tahoma", Font.BOLD, 14));
	lblEnterCourseName.setBounds(10, 136, 144, 23);
	add(lblEnterCourseName);
	
	spinner = new JSpinner();
	spinner.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
	spinner.setModel(new SpinnerDateModel(new Date(1339102800000L), new Date(1339102800000L), null, Calendar.DAY_OF_YEAR));
	spinner.setBounds(205, 171, 140, 20);
	add(spinner);
	
	JLabel lblChooseExecutionTime = new JLabel("Choose Execution Time");
	lblChooseExecutionTime.setFont(new Font("Tahoma", Font.BOLD, 14));
	lblChooseExecutionTime.setBounds(10, 168, 172, 22);
	add(lblChooseExecutionTime);
	
	lblEnterDuration = new JLabel("Enter Duration");
	lblEnterDuration.setFont(new Font("Tahoma", Font.BOLD, 14));
	lblEnterDuration.setBounds(10, 216, 105, 23);
	add(lblEnterDuration);
	
	textField = new JTextField();
	textField.setBounds(196, 219, 106, 21);
	add(textField);
	textField.setColumns(10);
	
	comboBox = new JComboBox();
	comboBox.setBounds(52, 297, 106, 20);
	Subject s = new Subject((String)subcmbx.getSelectedItem());
	SystemObject system = new SystemObject(s, SysMode.getQuestionsbysid);
	control.sendToServer(system);

	List<Question> qstList = null;
	SystemObject ret = control.GetSysObj();
	if (ret.getMode() == SysMode.OK)
		qstList = (List<Question>)ret.getObj();
	if (qstList != null)
	{
		for (int i=0;i<qstList.size();i++)
		{
			comboBox.addItem(qstList.get(i).getText());
		}
	}
	add(comboBox);
	
	btnAdd = new JButton("Add");
	btnAdd.setFont(new Font("Tahoma", Font.BOLD, 13));
	//btnAdd.addMouseListener(this);
	btnAdd.addActionListener(this);
	btnAdd.setBounds(342, 295, 89, 23);
	add(btnAdd);
	
	textField_4 = new JTextField();
	textField_4.setBounds(194, 297, 86, 20);
	add(textField_4);
	textField_4.setColumns(10);
	
	JButton btnUpload = new JButton("Upload");
	btnUpload.setFont(new Font("Tahoma", Font.BOLD, 13));
	btnUpload.setBounds(213, 425, 89, 23);
	add(btnUpload);

}

public void mouseClicked(MouseEvent event)
{
	
}
@Override			
public void actionPerformed(ActionEvent e) 
{
	if(e.getSource() == btnBack_1)
	{
		MainControl.setGUI(MainControl.currentUser.getRole(), this);
	}
	if(e.getSource()==btnAddExam)
	{
		Map<String, String> q = new HashMap<String, String>();
			//**get question text from combo box**//
			String qtext =(String) comboBox.getSelectedItem();
			//**get number of points for each Question from txt field**//
			String pts = textField_4.getText();
			q.put(qtext, pts);
			JOptionPane.showMessageDialog(null,"Question add successfully","System Message",JOptionPane.INFORMATION_MESSAGE);
			textField_4.setText(null);
			comboBox.remove(comboBox.getSelectedIndex());
		
		//LogicalExam logicE = new LogicalExam(textField_2.getText(), )
		//**get subject choosen from combo box**//
		String sub=(String)subcmbx.getSelectedItem();
		//**get date from spinner **//
		String date=(String) spinner.getValue().toString();
		//**get duration from txt field**//
		String drn = textField.getText();
		
		exam = new PracticalExam(textField_2.getText(), sub , date , textField_3.getText() , drn);
		control = new AddExamControl();
		SystemObject sys = new SystemObject(exam, SysMode.AddExam);
		control.sendToServer(sys);
		SystemObject retObj = control.GetSysObj();
		if (retObj.getMode() == SysMode.OK)
			{
			JOptionPane.showMessageDialog(null,"Insert Success","System Message",JOptionPane.INFORMATION_MESSAGE);
			MainControl.setGUI(MainControl.currentUser.getRole(), this);
			}
	}
	if(e.getSource()==btnAdd)
	{

	}
	}
}